from django import http
from django.shortcuts import render
from django.views import View

# Create your views here.
from users.models import User


class UsernameCountView(View):
    def get(self, request, username):
        """
        判断用户名是否重复注册
        :param request:
        :param username:
        :return:
        """
        count = User.objects.filter(username=username).count()

        return http.JsonResponse({'code': 0,
                                  'errmsg': 'ok',
                                  'count': count})


#
# class MobileCountView(View):
#     def get(self, request, mobile):
#         count = User.objects.filter(mobile=mobile).count()
#
#         return http.JsonResponse({'code': 0,
#                                   'errmsg': 'ok',
#                                   'count': count})

class MobileCountView(View):

    def get(self, request, mobile):
        '''
        判断电话是否重复, 返回对应的个数
        :param request:
        :param mobile:
        :return:
        '''
        # 1.从数据库中查询 mobile 对应的个数
        count = User.objects.filter(mobile=mobile).count()

        # 2.拼接参数, 返回
        return http.JsonResponse({'code': 0,
                                  'errmsg': 'ok',
                                  'count': count})
